package org.example.demo20231101.server.service;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.demo20231101.server.controller.vo.UserVO;
import org.example.demo20231101.server.domain.User;
import org.example.demo20231101.server.domain.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @version 1.0
 * @Author Lmh
 * @Description
 * @CreateTime 2023-11-01 21:45
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class UserService {

    private final UserRepository userRepository;

    @Transactional(rollbackFor = Exception.class)
    public void add(UserVO user) {
        userRepository.save(BeanUtil.copyProperties(user, User.class));
    }

    @Transactional(rollbackFor = Exception.class)
    public UserVO getById(Long id) {
        User referenceById = userRepository.getOne(id);
        return BeanUtil.copyProperties(referenceById, UserVO.class);
    }
}
